; If you meet compile error, try 'sudo apt install gcc-multilib g++-multilib' first

%include "head.include"

; your code here

your_if:
    ; if a1 < 12 then
    ;     if_flag = a1 / 2 + 1
    ; else if a1 < 24 then
    ;     if_flag = (24 - a1) * a1
    ; else
    ;     if_flag = a1 << 4
    ; end

    mov ax, word [a1] ; ax = a1
    cmp ax, 12         ; a1 < 12
    jl if_1            ; if a1 < 12 then
    cmp ax, 24         ; a1 < 24
    jl if_2            ; else if a1 < 24 then
    jmp if_3           ; else

if_1:                  ; if a1 < 12 then
    push ax            ; 保存ax
    mov ax, word [a1]  ; ax = a1
    shr ax, 1          ; ax = a1 / 2
    inc ax             ; ax = a1 / 2 + 1
    mov word [if_flag], ax ; if_flag = a1 / 2 + 1
    pop ax             ; 恢复ax

if_2:                  ; else if a1 < 24 then
    push ax            ; 保存ax
    mov ax, 24         ; ax = 24
    sub ax, word [a1]  ; ax = 24 - a1
    imul ax, word [a1] ; ax = (24 - a1) * a1
    mov word [if_flag], ax ; if_flag = (24 - a1) * a1
    pop ax             ; 恢复ax
    jmp if_end         ; end

if_3:                  ; else
    push ax            ; 保存ax
    mov ax, word [a1]  ; ax = a1
    shl ax, 4          ; ax = a1 << 4
    mov word [if_flag], ax ; if_flag = a1 << 4
    pop ax             ; 恢复ax
    jmp if_end         ; end

if_end:                ; end

your_while:
    ; while a2 >= 12 then
    ;     call my_random        // my_random将产生一个随机数放到eax中返回
    ;     while_flag[a2 - 12] = eax
    ;     --a2
    ; end
    mov eax, dword [a2] ; eax store a2
    mov ebx, [while_flag] ; ebx store while_flag
    cmp eax, 12         ; while a2 >= 12
    jl while_end        ; end
    mov esi, eax        ; esi store a2
    call my_random      ; eax = my_random()
    mov [ebx + esi - 12], al ; while_flag[a2 - 12] = eax
    dec esi             ; --a2
    mov dword [a2], esi ; a2 = esi
    jmp your_while      ; 循环

while_end:

%include "end.include"

your_function:
    ; your_function:
    ;     for i = 0; string[i] != '\0'; ++i then
    ;         pushad
    ;         push string[i] to stack
    ;         call print_a_char
    ;         pop stack
    ;         popad
    ;     end
    ;     return
    ; end
    xor ecx, ecx        ; 初始化i
    mov al, '1'         ; 用于存放string[i]

loop:
    pushad              ; 保存寄存器
    mov ebx, [your_string] ; ebx存放string
    mov esi, ecx        ; esi存放i
    mov eax, [ebx + esi] ; eax = string[i]
    cmp al, 0           ; string[i] != '\0'
    je out              ; end
    push eax            ; push string[i] to stack
    call print_a_char   ; call print_a_char
    pop eax             ; pop stack
    popad               ; popad
    inc ecx             ; ++i
    jmp loop            ; 循环

out:
    popad               ; 恢复寄存器
